package com.gopay.bis.hnapay.ebank.operation;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.hnapay.ebank.HnaPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.MD5Util;
import com.gopay.remote.bank.ICertHelper;
/**
 * 新生在线网银直连接口处理
 * @author LiLiLi
 *
 */
public class HnaPayCertHelper implements ICertHelper {
	public String sign(BaseMessage bsaeMessage) throws GopayException {
		try{
			HnaPayMessage hnapayMessage = (HnaPayMessage)bsaeMessage;
			BankLogUtils.printB2CSignPlainLog(hnapayMessage);
			String signMsg =  MD5Util.signType(hnapayMessage.getPlainMessage(), "UTF-8");
			BankLogUtils.printB2CSignSignatureLog(hnapayMessage, signMsg);
			return signMsg;
		} catch (Exception e) {
			BankLogUtils.printB2CSignExceptionLog(bsaeMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
		}
	}

	public Object verify(BaseMessage bsaeMessage) throws GopayException {
		try{
			HnaPayMessage hnapayMessage = (HnaPayMessage)bsaeMessage;
			BankLogUtils.printB2CVerifyPlainLog(hnapayMessage);
			BankLogUtils.printB2CVerifySignatureLog(hnapayMessage, hnapayMessage.getSignMsg());
			String signMessage = MD5Util.signType(hnapayMessage.getPlainMessage(),"UTF-8");
			if (!StringUtils.equals(signMessage, hnapayMessage.getSignMsg())) {
				return false;
			}
			return true;
		} catch (Exception e) {
			BankLogUtils.printB2CVerifyExceptionLog(bsaeMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
		}
	}

}
